package com.ulook.lookplanet.controller;

import com.alibaba.druid.util.StringUtils;
import com.ulook.lookplanet.bean.*;
import com.ulook.lookplanet.common.ExceptionCode;
import com.ulook.lookplanet.common.JsonResult;
import com.ulook.lookplanet.service.ArticleService;
import com.ulook.lookplanet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/4/16
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService service;

    /**
     * 获取文章的图片
     */
    @PostMapping("/getArtPic")
    public JsonResult<List<TArtPic>> getArtPic(HttpServletRequest request){
        String artid = request.getParameter("artid");
        if(artid == null){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        List<TArtPic> artPics = service.getArtPic(artid);
        if(artPics.size() > 0){
            return new JsonResult<>(artPics);
        }
        return new JsonResult<>(ExceptionCode.NO_DATA, "未查询到图片");
    }

    /**
     * 点赞
     */
    @PostMapping("/like")
    public JsonResult like(HttpServletRequest request){
        String userid = request.getParameter("userid");
        String artid = request.getParameter("artid");
        boolean isCancel = (request.getParameter("isCancel")) != null;
        TLikes likes = new TLikes();
        likes.setUserid(Integer.parseInt(userid));
        likes.setArtid(artid);
        System.out.println(likes);
        service.like(likes, isCancel);
        return new JsonResult();
    }

    /**
     * 收藏
     */
    @PostMapping("/collect")
    public JsonResult collect(HttpServletRequest request){
        String userid = request.getParameter("userid");
        String artid = request.getParameter("artid");
        boolean isCancel = (request.getParameter("isCancel")) != null;
        TCollect collect = new TCollect();
        collect.setUserid(Integer.parseInt(userid));
        collect.setArtid(artid);
        service.collect(collect, isCancel);
        return new JsonResult();
    }

    /**
     * 添加浏览记录
     */
    @PostMapping("/addView")
    public JsonResult addView(HttpServletRequest request){
        String userid = request.getParameter("userid");
        String artid = request.getParameter("artid");
        if(StringUtils.isEmpty(userid) || StringUtils.isEmpty(artid)){
            return new JsonResult(ExceptionCode.NULL_PARAM, "参数为空");
        }
        service.addView(Integer.parseInt(userid), artid);
        return new JsonResult();
    }

    /**
     * 查询点赞、收藏、评论数量
     */
    @PostMapping("/getArtCount")
    public JsonResult<VArtCount> getArtCount(HttpServletRequest request){
        String artId = request.getParameter("artId");
        if(StringUtils.isEmpty(artId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        VArtCount artCount = service.getArtCount(artId);
        if(artCount == null){
            return new JsonResult<>(ExceptionCode.NO_DATA, "未查询到数据");
        }
        return new JsonResult<>(artCount);
    }

    /**
     * 评论
     */
    @PostMapping("/comment")
    public JsonResult<VCommentInfo> comment(HttpServletRequest request){
        String artid = request.getParameter("artid");
        String userid = request.getParameter("userid");
        String content = request.getParameter("content");
        if(StringUtil.isEmpty(artid) || StringUtil.isEmpty(userid) || "0".equals(userid)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数错误");
        }
        TComment comment = new TComment();
        String uid = UUID.randomUUID().toString();
        comment.setUid(uid);
        comment.setArtid(artid);
        comment.setUserid(Integer.parseInt(userid));
        comment.setContent(content);
        String upid = request.getParameter("upid");
        if(upid != null){
            comment.setUpid(upid);
            comment.setReplyuser(Integer.parseInt(request.getParameter("replay")));
        }
        VCommentInfo vCommentInfo = service.comment(comment);
        System.out.println(vCommentInfo.toString() );
        return new JsonResult<>(vCommentInfo);
    }

    /**
     * 获取评论
     */
    @PostMapping("/getComment")
    public JsonResult<List<VCommentInfo>> getComment(HttpServletRequest request){
        String artId = request.getParameter("artid");
        if(StringUtils.isEmpty(artId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        String itemNum = request.getParameter("itemNum");
        int itemNum2 = itemNum==null ? 0 : Integer.parseInt(itemNum);
        List<VCommentInfo> commentInfos = service.getComment(artId, itemNum2);
        return new JsonResult<>(commentInfos);
    }

    /**
     * 获取话题
     */
    @PostMapping("/getTopicByComId")
    public JsonResult<List<TTopic>> getTopicByComId(HttpServletRequest request){
        String artId = request.getParameter("artId");
        if(StringUtil.isEmpty(artId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        List<TTopic> topics = service.getTopicByArtId(artId);
        return new JsonResult<>(topics);
    }

    /**
     * 模糊查询动态
     */
    @PostMapping("/searchArt")
    public JsonResult<List<VArticlePreInfo>> searchArt(HttpServletRequest request){
        String currUserID = request.getParameter("currUserId");
        if(currUserID == null || "".equals(currUserID)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        String itemNum = request.getParameter("itemNum");
        String s = request.getParameter("s");
        List<VArticlePreInfo> articlePreInfos = service.searchArt("%"+s+"%", Integer.parseInt(currUserID), Integer.parseInt(itemNum));
        if(articlePreInfos.size() == 0){
            return new JsonResult<>(ExceptionCode.NO_DATA, "没有更多动态了");
        }
        return new JsonResult<>(articlePreInfos);
    }
}
